<?php if(DEBUG){ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="<?=$this->baseUrl?>/public/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="<?=$this->baseUrl?>/public/css/icons.css" /> 
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/ext-base.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/ext-all.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/XmlTreeLoader.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/menu/EditableItem.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/menu/RangeMenu.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/GridFilters.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/Filter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/StringFilter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/DateFilter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/ListFilter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/NumericFilter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/filter/BooleanFilter.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/lightbox.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/RowExpander.js"></script>
<script type="text/javascript" src="<?=$this->baseUrl?>/public/js/ben/showresult.js"></script>
<? } ?>
<?php $ExtId=uniqid(); ?>
<script type="text/javascript">

Ext.QuickTips.init();
Ext.BLANK_IMAGE_URL = '<?=$this->baseUrl?>/public/images/default/s.gif';
var baseUrl="<?=$this->baseUrl?>";
var ExtId="<?=$ExtId?>";
var link_industry_tree_title="行业(通过右键菜单操作)";
var link_industry_tree_root_title="行业";




Ext.onReady(function(){
					 Ext.namespace('Folder');
// 刷新文件夹
Folder.refreshnode = function(node){
	node.reload();
};
// 添加文件夹
Folder.add = function(node){
		Ext.MessageBox.prompt('添加行业','请输入行业名称：',function(id,name){
			if(id=='ok'){
				Ext.Ajax.request({
					url:baseUrl+"/link/serverindustry/type/addIndustry",
					method:'POST',
					params:{name:name,top_id:node.id},
					callback: function(options, success, response) {
							if (success) {
									var result = Ext.util.JSON.decode(response.responseText);
									if (result.success) Folder.refreshnode(node);
									else Ext.MessageBox.alert('出现错误', result.info);
							} else {
									Ext.MessageBox.alert('出现错误', "网络或服务器出现故障！");
							}
					}
				});
			}			
		});
};
// 修改文件夹
Folder.rename = function(node){
	if (!node.parentNode) return;
		Ext.MessageBox.prompt('修改名称','请输入新名称：',function(id,name){
			if(id=='ok' && name!=''){
				Ext.Ajax.request({
					url:baseUrl+"/link/serverindustry/type/editIndustry",
					method:'POST',
					params:{name:name,id:node.id},
					callback: function(options, success, response) {
							if (success) {
									var result = Ext.util.JSON.decode(response.responseText);
									if (result.success) node.setText(name);
									else Ext.MessageBox.alert('出现错误', result.info);
							} else {
									Ext.MessageBox.alert('出现错误', "网络或服务器出现故障！");
							}
					}
				});
			}			
		},this,false,node.text);
};
// 删除文件夹
Folder.del = function(node){
	if (!node.parentNode) return;
	Ext.MessageBox.confirm('删除提示','确认要删除行业['+node.text+']吗？：',function(id,name){
		if(id=='yes'){
			Ext.Ajax.request({
				url:baseUrl+"/link/serverindustry/type/delIndustry",
				method:'POST',
				params:{id:node.id},
				callback: function(options, success, response) {
						if (success) {
								var result = Ext.util.JSON.decode(response.responseText);
								if (result.success) Folder.refreshnode(node);
								else Ext.MessageBox.alert('出现错误', result.info);
						} else {
								Ext.MessageBox.alert('出现错误', "网络或服务器出现故障！");
						}
				}
			});
		}			
	});
};
// 移动文件夹
Folder.move = function(tree, node, oldParent, newParent, index){
	// check same name
	var nodes = newParent.childNodes;
	for(key in nodes){
		if(nodes[key].text == node.text){
			//Ext.MessageBox.alert('提示','目标文件夹下已存在同名文件夹');
			// 只是稍微排序，没有实质变化
			return true;
		}
	}

	// 开始进行移动操作，为了避免错误，暂停用户操作
	var myMask = new Ext.LoadMask("link_industry_tree", {
			msg: "正在操作，请稍候..."
	});
	myMask.show();
	Ext.Ajax.request({
		url:baseUrl+"/link/serverindustry/type/dragIndustry",
		method:'POST',
		params:{id:node.id,new_top_id:newParent.id,name:node.text},
		callback: function(options, success, response) {				
        myMask.hide();
				if (success) {
						var result = Ext.util.JSON.decode(response.responseText);
						if (result.success){
							newParent.reload();// 主要是更新node的id值
						}else{
							// 提示错误，并更新目录（如果目录树很长，可以考虑仅还原移动的结点位置-通过判断node.parentNode和newParent）
							Ext.MessageBox.alert('出现错误', result.info);
							oldParent.reload();
							newParent.reload();
						}
				} else {
						Ext.MessageBox.alert('出现错误', "网络或服务器出现故障！");
				}
		}
	});
	
	return true;
}



var tree=new Ext.tree.TreePanel({
	id		: 'link_industry_tree',
	renderTo: 'tree_'+ExtId,
	autowidht	: true,
	title	: link_industry_tree_title,
	animate	: true,
	border	: true,
	enableDD: true,
	containerScroll:true,
	rootVisable:true,
	bodyStyle:'padding:10px',
	loader:new Ext.tree.TreeLoader({
								   dataUrl:baseUrl+"/link/serverindustry/type/dataindustry"
								   }),
	
	root: {
            text: link_industry_tree_root_title,
            draggable:false,
            id:'0'
        },
	listeners:{
		beforemovenode: Folder.move
		}
	});

    tree.render();
    tree.getRootNode().expand();
	
	//tree.on("click",function(node,e){
//							 if(node.isLeaf()){
//								 e.stopEvent();
//								 }
//							else{
//								node.toggle();
//								}
//							 });
	
	tree.on("contextmenu",function(node,e){
	var treeMenu=new Ext.menu.Menu({
			id:'link_industry_tree_contentmenu',
			doAction:function(a,b){
					var a = eval('Folder.'+a);
					a(node);
				},
			items:[{
            			id: 'refreshnode',
						iconCls:'icn-refresh',
            			text: '刷新'
        			},{
            			id: 'add',
						iconCls:'icn-folder-new',
            			text: '新建行业'
        			},{
            			id: 'rename',
						iconCls:'icn-rename',
           				text: '修改名称'
        			},{
            			id: 'del',
						iconCls:'icn-remove',
            			text: '删除行业'
        		}],
			 listeners: {
            itemclick: function(item) {
							var n = item.parentMenu.contextNode;
							this.doAction(item.id,n);
						}
        }
		});
		treeMenu.showAt(e.getPoint());
		var items = treeMenu.items.items;
		if(!node.parentNode){
						items[2].disable();
						items[3].disable();
					}else{
						items[2].enable();
						items[3].enable();
					}

	},tree);
	
	
	
		

	
});

</script>
<body style="width:98%;height:100%">
<div id='tree_<?=$ExtId?>'></div>
</body>
</html>